package com.snj.visangedu.drm;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.snj.visangedu.R;
import com.teruten.tms4encrypt.TMS4Encrypt;
import com.teruten.tms4webserver.TMS4WebServer;
import com.teruten.tplayer.TPlayer;
import com.teruten.tplayer.view.TPlayerActivity;

public class TPlayAct extends TPlayerActivity {

	private static final String TAG = "TPlayerAct";
	private TPlayer _playerControl;					// TPlayer
	TMS4WebServer _webServer;						// DRM

	@SuppressLint("DefaultLocale")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		String strMediaPath = getIntent().getStringExtra("PATH");
		String strFileName = new File(strMediaPath).getName();
		String strTitle = getIntent().getStringExtra("TITLE");
		Log.e(TAG, "Player, path: " + strMediaPath + ", title: " + strTitle);

		/***************************** DRM ***********************/
		if (strMediaPath.endsWith(".MS4")) {
			int nReturn;
			
			// 
			if(DRM_Init() != TMS4Encrypt.TMS4E_OK) return;

			// 
			if (strMediaPath.startsWith("http://")) {
				nReturn = _webServer.OpenRemoteMedia(strMediaPath);
				if(nReturn != TMS4Encrypt.TMS4E_OK)
				{
					displayMessage("open remote media error : " + nReturn);
					return;
				}
			}
			// 
			else 
			{
				int nTime = (int) (System.currentTimeMillis() / 1000);
				nReturn = _webServer.MediaAccreditation(strMediaPath, nTime);
				if (nReturn != 0) {
					displayMessage("open local media error : " + nReturn);
					Log.d(TAG, "Faile Accredit : " + Integer.toString(nReturn));
					return;
				}
				DRM_showInfo();
			}
			
			// DRM
			strMediaPath = _webServer.getFilePath(strMediaPath);
		}
		
		
		

		/***************************** Player ***********************/

		// 1.player
		_playerControl = new TPlayer(this, TPlayer.LOCALE_KOR);
		_playerControl.SetTPlayerUI(true, true, true);
		BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.teruten);
		_playerControl.SetCI(drawable.getBitmap(), 100);
		//_playerControl.SetWatermark("TERUTEN");

		String[] arrString = _playerControl.GetBookmarkList(strFileName);
		for(int i = 0; i < arrString.length; i++)
			Log.i(TAG, "BookMark : " + arrString[i]);
		
		

		// 
		_playerControl.SetOnBookmarkAddCb(new TPlayer.OnBookmarkAddCb() {
			@Override
			public void OnBookmarkAdd(int index, int offset, String path) {
				Log.v(TAG, "占싹몌옙크 占쌩곤옙 : " + index + "\r\n offset : " + offset + "\r\n path : " + path);
			}
		});

		// 
		_playerControl.SetOnBookmarkDeleteCb(new TPlayer.OnBookmarkDeleteCb() {

			@Override
			public void OnBookmarkDelete(int index, int offset, String path) {
				Log.v(TAG, "占싹몌옙크 占쏙옙占쏙옙 : " + index + "\r\n offset : " + offset + "\r\n path : " + path);
			}
		});

		// 
		_playerControl.SetOnResumeVideoCb(new TPlayer.OnResumeVideoCb() {

			public void OnResumeVideo(String strFileName, int nDuration,
					int nPosition) {
				Log.v(TAG, "占싱어보占쏙옙 : " + strFileName + ", duration : " + nDuration + ", position : " + nPosition);
			}
		});

		// 
		_playerControl.SetOnDestroyVideoCb(new TPlayer.OnDestroyVideoCb() {

			public void OnDestroyVideo(String strFileName, int nDuration,
					int nPosition) {
				Log.v(TAG, "占쏙옙占쏙옙 : " + strFileName + ", duration : " + nDuration	+ ", position : " + nPosition);
				//finish();
			}
		});

		
		if(!_playerControl.PlayMedia(strMediaPath, strTitle, 0))
			displayMessage("error!!!!!!!!!!!!");
		
		
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		Log.e(TAG, "video act onDestroy");
		super.onDestroy();
		if(_webServer != null)
		{
			try {
				_webServer.StopServer();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			_webServer = null;
		}
		
	}
	
	/**********************************************************
	 * DRM 
	**********************************************************/
	private int DRM_Init() {
		if (_webServer == null) {
			Log.e(TAG, "web server start");
			_webServer = new TMS4WebServer();
			DRMINFO info = DRMINFO.getInstance();
			int nReturn = _webServer.InitInstance(info.IMEI, info.MACADDRESS, this);
			if(nReturn != TMS4Encrypt.TMS4E_OK)
			{
				displayMessage("DRM init err : " + nReturn);
				Log.e(TAG, "DRM init err : " + nReturn);
				return nReturn;
			}
			_webServer.StartServer(50000);
		}
		return TMS4Encrypt.TMS4E_OK;
	}

	private void DRM_showInfo()
	{
		int nEndTime = _webServer.GetEndTime();
		int nCountPolicy = _webServer.GetPolicyCount();
		int nPolicyType = _webServer.GetPolicyType();

		if (nPolicyType == 0x102) // 
		{
			Log.d(TAG, "policy type : period");
			if (nEndTime > 0) {
				long lTime = (long) nEndTime * 1000;
				Log.d(TAG, "time : " + Long.toString(lTime) + ", "+ Integer.toString(nEndTime));
				Date date = new Date(lTime);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.KOREA);
				displayMessage(format.format(date) + " ");
			}
		}
		if (nPolicyType == 0x104) // 
		{
			if (nCountPolicy > 0) {
				Log.d(TAG, "policy type : count");
				displayMessage(" " + nCountPolicy + " 회");
			}
		}
		if (nPolicyType == (0x102 | 0x104)) // 
		{
			Log.d(TAG, "period + count");
			long lTime = (long) nEndTime * 1000;
			Log.d(TAG, "time : " + Long.toString(lTime) + ", "+ Integer.toString(nEndTime));
			Date date = new Date(lTime);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.KOREA);
			if ((nEndTime > 0) && (nCountPolicy > 0))
				displayMessage(format.format(date) + "  "
						+ nCountPolicy + " ");
		}
	}

	
			
	public void displayMessage(String message){
		if(message != null){
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}
}
